package com.alibaba.citrus.ots.common.util;

import com.alibaba.cz.base.tool.converter.simple.B;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Chen.mu
 * SAP货品数量同步 乘1000的处理工具类
 * @date 2021/12/6
 */
public class QuantityFormatUtils {

    private static final BigDecimal formatUnit=new BigDecimal(1000);

    /**
     * 数据库的数量，回显的时候要除1000
     * @param quantityInDb
     * @return
     */
    public static BigDecimal getQuantityForPage(BigDecimal quantityInDb){
        if(quantityInDb==null){
            return new BigDecimal(0);
        }
        BigDecimal forShow=quantityInDb.divide(formatUnit,3,BigDecimal.ROUND_DOWN);
        return forShow;
    }
    public static BigDecimal getQuantityForPage(String quantityInDb){
        if(StringUtils.isBlank(quantityInDb)){
            return new BigDecimal(0);
        }
        BigDecimal forShow=new BigDecimal(quantityInDb).divide(formatUnit,3,BigDecimal.ROUND_DOWN);
        return forShow;
    }

    /**
     * 数据库的数量，存的时候要乘1000
     * 给的就3位小数，给多了乘1000以后还有小数 也质保流3位
     * @param quantityforDb
     * @return
     */
    public static BigDecimal formatQuantityForDB(BigDecimal quantityforDb){
        if(quantityforDb==null){
            return new BigDecimal(0);
        }
        BigDecimal forShow=quantityforDb.multiply(formatUnit).setScale(3,RoundingMode.HALF_UP);
        return forShow;
    }
    public static BigDecimal formatQuantityForDB(String quantityforDb){
        if(StringUtils.isBlank(quantityforDb)){
            return new BigDecimal(0);
        }
        BigDecimal forShow=new BigDecimal(quantityforDb).multiply(formatUnit).setScale(3,RoundingMode.HALF_UP);
        return forShow;
    }
}
